<?php

class FacturaClient {
	public $id_factura;
	public $id_client;
	public $nr_factura;
	public $data; 
	public $suma;
	public $moneda;
	public $data_inceput;
	public $data_sfarsit;
	public $achitat;			//0 - neachitat; 1 - achitat
	
	function __construct($id_client, $nr_factura, $data, $suma, $moneda, $data_inceput, $data_sfarsit, $achitat) {
		$this->id_client = $id_client;
		$this->nr_factura = $nr_factura;
		$this->data = $data;
		$this->suma = $suma;
		$this->moneda = $moneda;
		$this->data_inceput = $data_inceput;
		$this->data_sfarsit = $data_sfarsit;
		$this->achitat = $achitat;
	}
	
	public function queryInsertFactura($db) {
		$query = $db->prepare("INSERT INTO `factura_client`  (	`id_client` , 				`nr_factura` , 
												 				`data` , 					`suma`,
	                        		        	 				`moneda` ,    				`data_inceput`,
	                        		        	 				`data_sfarsit` 		                        	      
		                               						) 
		            							VALUES  	(	:id_client,  		:nr_factura, 
		            											:data,   			:suma, 
		                      									:moneda, 			:data_inceput,
		                      									:data_sfarsit
		                      								)");
		
		$query->bindParam(':id_client', 	($this->id_client));
		$query->bindParam(':nr_factura', 	($this->nr_factura));
		$query->bindParam(':data', 			($this->data));
		$query->bindParam(':suma', 			($this->suma));
		$query->bindParam(':moneda', 		($this->moneda));
		$query->bindParam(':data_inceput', ($this->data_inceput));
		$query->bindParam(':data_sfarsit', ($this->data_sfarsit));
						
		if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
	  		$query->closeCursor();
			throw new QueryInsertFacturaException("Failed saving factura to the database" . $errInfo);
		}
		
  		$this->id_factura = (int) $db->lastInsertId();
  		$query->closeCursor();
		return $this->id_factura; 		//return the Factura's id
	}
	
  	public static function queryLoadFactura($db, $id_client, $id_factura) {
        $query = $db->prepare("SELECT * FROM factura_client
                    				WHERE id = :id_factura
                                    AND id_client = :id_client");
        
        $query->bindParam(':id_factura', $id_factura);
        $query->bindParam(':id_client', $id_client);
        
        if ($query->execute() === FALSE) { 
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
        	$query->closeCursor();
        	throw new QueryLoadFacturaException("Failed loading factura with id: " . $id_factura . $errInfo);   				
        }
        
        $data = $query->fetch(PDO::FETCH_ASSOC);            
                
        if (!isset($data['id'])) {
        	$query->closeCursor();
        	throw new QueryLoadFacturaException("Failed loading factura with id: " . $id_factura . " reason: id not existing");        	
        }
                               
        $factura = new FacturaClient($data['id_client'], $data['nr_factura'], $data['data'], $data['suma'], $data['moneda'] 
        							, $data['data_inceput'], $data['data_sfarsit'], $data['achitat']);
        $factura->id_factura = (int) $data['id'];
        $query->closeCursor();
        return $factura;
    }	
    
    //deletes a "factura" by id; does not require an object.... STATIC
    public static function deleteFactura($db, $id_factura) {
    	$query = $db->prepare("DELETE FROM factura_client WHERE id = :id_factura");
    	$query->bindParam(':id_factura', $id_factura);
    	    	
      	if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
      		$query->closeCursor();
      		throw new QueryDeleteFacturaException("Faild deleting factura with id: " . $this->id_factura . $errInfo);    	
      	}
    	
      	$query->closeCursor();
	  	return CT_RESULT_OK;      	
    }

    public static function queryLoadByClient($db, $id_client) {
        $query = $db->prepare("SELECT *
                    			FROM factura_client
                    			WHERE id_client = :id_client");
        $query->bindParam(':id_client', $id_client);
                            			        
        
        if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
      		$query->closeCursor();
        	throw new QueryLoadFacturaException("Failed loading factura for client with id: " . $id_client . $errInfo);   				
        }
        
        $facturi = array();
        
        while ($data = $query->fetch(PDO::FETCH_ASSOC)) {                        
            $factura = new FacturaClient($data['id_client'], $data['nr_factura'], $data['data'], $data['suma'], $data['moneda'] 
        							, $data['data_inceput'], $data['data_sfarsit'], $data['achitat']);
            $factura->id_factura = (int) $data['id'];
            $facturi[] = $factura;
        }        
        
        $query->closeCursor();
        return $facturi;
    }
    
    public static function getFacturiNeachitate($db) {
        $query = $db->prepare("SELECT f.data_inceput, f.data_sfarsit, f.data, f.nr_factura, f.id, c.nume, f.suma
                                FROM factura_client f
                                LEFT JOIN client c
                                    ON c.id = f.id_client
                                WHERE achitat = " . CT_FACTURA_NEACHITATA);
        
        if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
            $query->closeCursor();
            throw new QueryGetFacturiNeachitateException("Failed loading facturi neachitate " . $errInfo);                   
        }
        
        $facturi = array();
        $poz = 0;
        while ($data = $query->fetch(PDO::FETCH_ASSOC)) {                        
            $facturi[$poz]['id'] = $data['id'];
            $facturi[$poz]['data_inceput'] = $data['data_inceput'];
            $facturi[$poz]['data_sfarsit'] = $data['data_sfarsit'];
            $facturi[$poz]['data'] = $data['data'];
            $facturi[$poz]['nr_factura'] = $data['nr_factura'];
            $facturi[$poz]['nume'] = $data['nume'];
            $facturi[$poz]['suma'] = $data['suma'];
            $poz++;
        }                
        $query->closeCursor();
        return $facturi;    
    }
    
    public static function updateFacturaAsAchitata($db, $factura_id, $id_client, $data_sfarsit) {
    	try {
            $query = $db->prepare("UPDATE factura_client SET achitat = '" . CT_FACTURA_ACHITATA . "'
                                    WHERE id = $factura_id;"); 
            
            if ($query->execute() === FALSE) {
                $errInfo = $query->errorInfo();
                $errInfo = $errInfo[2];
                $query->closeCursor();
                throw new QueryUpdateFacturaAsAchitataException("Failed updating achitat=achitat for factura with id " . $factura_id . $errInfo);
            }
            $query->closeCursor();         
        }
        catch (QueryUpdateFacturaClientException $e) {
            throw new QueryUpdateFacturaAsAchitataException("Failed updating achitat=achitat for factura with id " . $factura_id . $errInfo);
        }             
    }
    
}
	
?>
